/*!
 * Copyright (C) 2020 - All Rights Reserved by
 * @author : ZhaoYanbo
 * @email  : zyb920@hotmail.com
 * @created: 2020-10-31
 * @version: 1.0.0.0
 *
 */

#include "le_command.h"
#include "lyriceditwidget.h"
#include <QtDebug>

LE_CommandInvok::~LE_CommandInvok()
{
    clear();
}

void LE_CommandInvok::push(LE_Command *ptr)
{
    if(ptr)
    {
        clearRedo();
        m_undo_list.append(ptr);
        LE_Command::Type type = ptr->type();
        if(type != LE_Command::Move)
        {
            ptr->redo();
        }
    }
}

void LE_CommandInvok::redo()
{
    if(m_redo_list.length() > 0)
    {
        LE_Command *ptr = m_redo_list.last();
        m_redo_list.removeLast();

        m_undo_list.append(ptr);
        ptr->redo();
    }
}

void LE_CommandInvok::undo()
{
    if(m_undo_list.length() > 0)
    {
        LE_Command *ptr = m_undo_list.last();
        m_undo_list.removeLast();

        m_redo_list.append(ptr);
        ptr->undo();
    }
}

void LE_CommandInvok::clear()
{
    clearRedo();
    clearUndo();
}

bool LE_CommandInvok::canUndo() const
{
    return (m_undo_list.length() > 0);
}

bool LE_CommandInvok::canRedo() const
{
    return (m_redo_list.length() > 0);
}

void LE_CommandInvok::clearUndo()
{
    if(m_undo_list.length() > 0)
    {
        qDeleteAll(m_undo_list);
        m_undo_list.clear();
    }
}

void LE_CommandInvok::clearRedo()
{
    if(m_redo_list.length() > 0)
    {
        qDeleteAll(m_redo_list);
        m_redo_list.clear();
    }
}


//-----
LE_MoveCommand::LE_MoveCommand(const QVector<VPos> &oldPos, const QVector<VPos> &newPos,
                               LyricEditWidget *wPtr) : LE_Command(wPtr)
{
    m_type = Move;
    m_oldPos.append(oldPos);
    m_newPos.append(newPos);
}

void LE_MoveCommand::redo()
{
    if(p_receiver) {
        p_receiver->loadFromValues(m_newPos);
    }
}

void LE_MoveCommand::undo()
{
    if(p_receiver) {
        p_receiver->loadFromValues(m_oldPos);
    }
}


//-----
LE_CanMoveCommand::LE_CanMoveCommand(int index, LyricEditWidget *wPtr) :
    LE_Command(wPtr)
{
    m_type = CanMove;
    m_index = index;
    if(p_receiver)
        m_bCanmove = p_receiver->canMove(index);
}

void LE_CanMoveCommand::redo()
{
    if(p_receiver)
        p_receiver->doClickCanMove(m_index);
}

void LE_CanMoveCommand::undo()
{
    if(p_receiver)
    {
        if(!m_bCanmove)
            p_receiver->doClickCanMove(m_index + 1);
        else
            p_receiver->doClickCanMove(m_index - 1);
    }
}

//-----
LE_PreviewCommand::LE_PreviewCommand(int oldIndex, int newIndex, LyricEditWidget *wPtr) :
    LE_Command(wPtr)
{
    m_type     = Preview;
    m_oldIndex = oldIndex;
    m_newIndex = newIndex;
}

void LE_PreviewCommand::redo()
{
    if(p_receiver)
        p_receiver->doClickPreview(m_newIndex);
}

void LE_PreviewCommand::undo()
{
    if(p_receiver)
        p_receiver->doClickPreview(m_oldIndex);
}

LE_ZoomCommand::LE_ZoomCommand(bool in, LyricEditWidget *wPtr) : LE_Command(wPtr)
{
    m_type = Zoom;
    m_isZoomIn = in;
}

void LE_ZoomCommand::redo()
{
    if( !p_receiver )
        return;

    if(m_isZoomIn)
        p_receiver->zoomInCommand();
    else
        p_receiver->zoomOutCommand();
}

void LE_ZoomCommand::undo()
{
    if( !p_receiver )
        return;

    if(m_isZoomIn)
        p_receiver->zoomOutCommand();
    else
        p_receiver->zoomInCommand();
}
